
#include <string.h>

#include "mm32_device.h"
#include "hal_conf.h"
#include "app_process.h"
#include "mm32_port.h"
#include "cmt2300a.h"

sys_status_t sys_status;

void key_init()
{
	GPIO_InitTypeDef GPIO_InitStruct;

    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);

    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_0 | GPIO_Pin_15 | GPIO_Pin_12 | GPIO_Pin_11;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_IPD;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOB, ENABLE);
    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_IPD;
    GPIO_Init(GPIOB, &GPIO_InitStruct);

    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOD, ENABLE);
    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_6 ;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_FLOATING;
    GPIO_Init(GPIOD, &GPIO_InitStruct);

    sys_status.on_off=1;
    sys_status.dog1=1;
    sys_status.dog_select=1;

    sys_status.voice=1;
    sys_status.voice_grade=1;
    sys_status.shock_grade=1;
    sys_status.electric_grade=1;
    sys_status.digital_value=sys_status.voice_grade;

    sys_status.neck_battery=80;
	sys_status.remote_battery=65;
}
void battery_check_init()
{
	ADC_InitTypeDef  ADC_InitStruct;
    GPIO_InitTypeDef GPIO_InitStruct;

    RCC_APB2PeriphClockCmd(RCC_APB2ENR_ADC1, ENABLE);

    ADC_StructInit(&ADC_InitStruct);
    ADC_InitStruct.ADC_Resolution         = ADC_Resolution_12b;
    ADC_InitStruct.ADC_PRESCARE           = ADC_PCLK2_PRESCARE_16;
    ADC_InitStruct.ADC_Mode               = ADC_Mode_Continue;
    ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
    ADC_InitStruct.ADC_ExternalTrigConv   = ADC1_ExternalTrigConv_T1_CC1;
    ADC_InitStruct.ADC_DataAlign          = ADC_DataAlign_Right;
    ADC_Init(ADC1, &ADC_InitStruct);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 0, ADC_Samctl_240_5);

    ADC_ANY_NUM_Config(ADC1, 1);
    ADC_ANY_CH_Config(ADC1, 1, ADC_Channel_5);
    ADC_ANY_Cmd(ADC1, ENABLE);

    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);

    /* PA5(RV1) PA4(RV2) PA1(RV3) */
    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_5;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AIN;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    ADC_Cmd(ADC1, ENABLE);

    ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}

void battery_check_process()
{
	static uint16_t t=0;
	if(t++>1000)
	{
		t=0;
		if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC))
	    {
			float RVxVoltage=(float)ADC_GetChannelConvertedValue(ADC1, ADC_Channel_5) * (float)3.3 / (float)4096.0;
			
			if(RVxVoltage>1.40)
			{
				sys_status.remote_battery_value=100*((RVxVoltage-1.4)/0.4);
			}
			else
			{
				sys_status.remote_battery_value=0;
				
			}

			ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
	    }
	}
	

        
}

void key_process()
{
	static uint32_t pressed1=0,pressed2;

	battery_check_process();
	if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0))
	{
		if(pressed1++==3000)
		{
			CMT2300A_GoSleep();
			NVIC_SystemReset();
		}
		
	}
	else
	{
		pressed1=0;
	}
	
	if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8))
	{
		sys_status.digital_value=sys_status.shock_grade;
		sys_status.voice=0;
		sys_status.shock=1;
		sys_status.electric=0;

		sys_status.new_cmd=SHOCK;
		sys_status.new_grade=sys_status.shock_grade;

		LED_B_1;
	}
	else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7))
	{
		sys_status.digital_value=sys_status.voice_grade;

		sys_status.voice=1;
		sys_status.shock=0;
		sys_status.electric=0;

		sys_status.new_cmd=VOICE;
		sys_status.new_grade=sys_status.voice_grade;

		LED_B_1;
	}
	else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6))
	{
		if(pressed2++==10)
		{
			if(sys_status.light)
			{
				sys_status.light=0;
			}
			else
			{
				sys_status.light=1;
			}
		}
		LED_B_1;
	}
	else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5))
	{
		sys_status.digital_value=sys_status.electric_grade;
		sys_status.voice=0;
		sys_status.shock=0;
		sys_status.electric=1;

		sys_status.new_cmd=ELECTRICE;
		sys_status.new_grade=sys_status.electric_grade;
		LED_B_1;
	}
	else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4))
	{
		if(pressed2++==10)
		{
			sys_status.dog_select++;
			if(sys_status.dog_select==1)
			{
				sys_status.dog1=1;
				sys_status.dog2=0;
				sys_status.dog3=0;

			}
			else if(sys_status.dog_select==2)
			{
				sys_status.dog1=0;
				sys_status.dog2=1;
				sys_status.dog3=0;

			}
			else if(sys_status.dog_select==3)
			{
				sys_status.dog1=0;
				sys_status.dog2=0;
				sys_status.dog3=1;
				sys_status.dog_select=0;
			}

		}
		LED_B_1;
		
	}
	else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3))
	{
		if(pressed2++==10)
		{
			if(sys_status.voice==1)
			{					
				if(sys_status.voice_grade>0)
				{
					sys_status.voice_grade--;						
				}
				if(sys_status.voice_grade==0)
				{
					sys_status.voice_grade=8;
				}
				sys_status.digital_value=sys_status.voice_grade;
			}
			else if(sys_status.shock==1)
			{
				if(sys_status.shock_grade>0)
				{
					sys_status.shock_grade--;						
				}
				if(sys_status.shock_grade==0)
				{
					sys_status.shock_grade=8;
				}
				sys_status.digital_value=sys_status.shock_grade;
			}
			else if(sys_status.electric==1)
			{
				if(sys_status.electric_grade>0)
				{
					sys_status.electric_grade--;						
				}
				if(sys_status.electric_grade==0)
				{
					sys_status.electric_grade=8;
				}
				sys_status.digital_value=sys_status.electric_grade;
			}
		}
		LED_B_1;
	}
	else if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_15))
	{
		if(pressed2++==10)
		{
			if(sys_status.voice==1)
			{
				sys_status.voice_grade++;
				if(sys_status.voice_grade>8)
				{
					sys_status.voice_grade=1;
				}
				sys_status.digital_value=sys_status.voice_grade;
			}
			else if(sys_status.shock==1)
			{
				sys_status.shock_grade++;
				if(sys_status.shock_grade>8)
				{
					sys_status.shock_grade=1;
				}
				sys_status.digital_value=sys_status.shock_grade;
			}
			else if(sys_status.electric==1)
			{
				sys_status.electric_grade++;
				if(sys_status.electric_grade>8)
				{
					sys_status.electric_grade=1;
				}
				sys_status.digital_value=sys_status.electric_grade;
			}
		}
		LED_B_1;
	}
	else
	{
		pressed2=0;
		LED_B_0;
	}
	if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12))
	{
		sys_status.lock=1;
	}
	else
	{
		sys_status.lock=0;
	}
	if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_11))
	{
		sys_status.gps=1;
	}
	else
	{
		sys_status.gps=0;
	}
		
	if(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_6))
	{
		static uint32_t n=0;

		if(n++>500)
		{
			n=0;
			if(sys_status.remote_battery_value<100)
			{
				sys_status.remote_battery+=20;
				if(sys_status.remote_battery>100)
				{
					sys_status.remote_battery=0;
				}
			}
			else
			{
				sys_status.remote_battery=100;
			}
			

		}

	}
	else
	{
		sys_status.remote_battery=sys_status.remote_battery_value;
	}
	
	if(sys_status.neck_battery_charging)
	{
		static uint32_t n=0;

		if(n++>500)
		{
			n=0;
			if(sys_status.neck_battery_value<100)
			{
				sys_status.neck_battery+=20;
				if(sys_status.neck_battery>100)
				{
					sys_status.neck_battery=0;
				}
			}
			else
			{
				sys_status.neck_battery=100;
			}
			

		}

	}
	else
	{
		sys_status.neck_battery=sys_status.neck_battery_value;
	}
	
	

}

